package com.ghbank.rdws.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ghbank.rdws.biz.repository.FetchDataConfigRepository;
import com.ghbank.rdws.biz.repository.SysDataSourceConfigRepository;
import com.ghbank.rdws.biz.service.RdwsService;
import com.ghbank.rdws.common.db.DataSourceFactory;
import com.ghbank.rdws.common.db.utils.DbUtils;
import com.ghbank.rdws.common.entity.FetchDataConfig;
import com.ghbank.rdws.common.entity.SysDataSourceConfig;
import com.ghbank.rdws.common.model.DataSourceConfig;
import com.ghbank.rdws.common.model.DbType;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RdwsServiceImpl implements RdwsService {

    @Autowired
    private SysDataSourceConfigRepository sysDataSourceRepository;

    @Autowired
    private FetchDataConfigRepository fetchDataConfigRepository;

    @Autowired
    private DataSourceFactory dataSourceFactory;

    public List<Map<String, Object>> queryForList(String fetchName, Map<String, Object> paramMap) {
        List<Map<String, Object>> result = null;
        FetchDataConfig fetchDataConfig = fetchDataConfigRepository.findByFetchName(fetchName);
        SysDataSourceConfig dataSourceConfig = sysDataSourceRepository.findById(fetchDataConfig.getId()).get();
        DbType dbType = DbType.valueOf(dataSourceConfig.getDbType().toUpperCase());
        DataSourceConfig config = DbUtils.getDataSourceConfig(dataSourceConfig, dbType);
        try {
            DataSource dataSource = dataSourceFactory.getDataSource(config);
            result = DbUtils.queryForList(dataSource, fetchDataConfig.getFetchSql(), paramMap);
        } catch (ExecutionException e) {
            e.printStackTrace();
            log.error("获取数据源失败", e);
        }
        return result;
    }

    public Map<String, Object> queryForMap(String fetchName, Map<String, Object> paramMap) {
        Map<String, Object> result = new HashMap<String, Object>();
        FetchDataConfig fetchDataConfig = fetchDataConfigRepository.findByFetchName(fetchName);
        if (fetchDataConfig == null) {
            log.error("没有找到{}对应的配置信息",fetchName);
            throw new RuntimeException("查询配置信息不存在");
        }
        SysDataSourceConfig dataSourceConfig =
            sysDataSourceRepository.findById(fetchDataConfig.getDataSourceId()).get();
        DbType dbType = DbType.valueOf(dataSourceConfig.getDbType().toUpperCase());
        DataSourceConfig config = DbUtils.getDataSourceConfig(dataSourceConfig, dbType);
        try {
            DataSource dataSource = dataSourceFactory.getDataSource(config);
            result = DbUtils.queryForObject(dataSource, fetchDataConfig.getFetchSql(), paramMap);
        } catch (ExecutionException e) {
            e.printStackTrace();
            log.error("获取数据源失败", e);
        }
        return result;
    }

    public String getFetchSql(String fetchName) {
        FetchDataConfig fetchDataConfig = fetchDataConfigRepository.findByFetchName(fetchName);
        return fetchDataConfig.getFetchSql();
    }

    public void removeDataSource(DataSourceConfig config) throws ExecutionException {
        dataSourceFactory.removeDataSource(config);
    }

    @Override
    public List<SysDataSourceConfig> queryDataSourceConfig(Pageable pageable) {
        Page<SysDataSourceConfig> page = sysDataSourceRepository.findAll(pageable);
        return page.getContent();
    }
}
